/**
 * Copyright &copy; 2016-2017 <a href="http://git.oschina.net/whoamien/backend_management">Backend Management</a> All rights reserved.
 */
package com.xw.util;

import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 *  
 * JSON转换工具类.
 *
 * @author Xi Wei
 */
/**
 * @author Xi Wei
 * @version 1.0
 * @since 1.0
 */
public class JsonUtil {
	private static ObjectMapper mapper;

	/**
	 * 获取ObjectMapper实例.
	 *
	 * @param createNew            方式：true，新实例；false,存在的mapper实例
	 * @return the mapper instance
	 */
	public static synchronized ObjectMapper getMapperInstance(boolean createNew) {
		if (createNew) {
			return new ObjectMapper();
		} else if (mapper == null) {
			mapper = new ObjectMapper();
		}
		return mapper;
	}

	/**
	 * 将java对象转换成json字符串.
	 *
	 * @param obj            准备转换的对象
	 * @return json字符串
	 */
	public static String beanToJson(Object obj) {
		try {
			ObjectMapper objectMapper = getMapperInstance(false);
			String json = objectMapper.writeValueAsString(obj);
			return json;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 将java对象转换成json字符串.
	 *
	 * @param obj            准备转换的对象
	 * @return json字符串
	 */
	public static String beanToJsonNotIncludeNull(Object obj) {
		try {
			ObjectMapper objectMapper = getMapperInstance(false);
			String json = objectMapper.writeValueAsString(obj);
			return json;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 将java对象转换成json字符串.
	 *
	 * @param obj            准备转换的对象
	 * @param createNew            ObjectMapper实例方式:true，新实例;false,存在的mapper实例
	 * @return json字符串
	 */
	public static String beanToJson(Object obj, Boolean createNew) {
		try {
			ObjectMapper objectMapper = getMapperInstance(createNew);
			String json = objectMapper.writeValueAsString(obj);
			return json;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 将json字符串转换成java对象.
	 *
	 * @param <T> the generic type
	 * @param json            准备转换的json字符串
	 * @param cls            准备转换的类
	 * @return the t
	 */
	public static <T> T jsonToBean(String json, Class<T> cls) {
		return jsonToBean(json, cls, true);
	}

	/**
	 * 将json字符串转换成java对象.
	 *
	 * @param <T> the generic type
	 * @param json            准备转换的json字符串
	 * @param cls            准备转换的类
	 * @param createNew            ObjectMapper实例方式:true，新实例;false,存在的mapper实例
	 * @return the t
	 */
	public static <T> T jsonToBean(String json, Class<T> cls, Boolean createNew) {
		try {
			ObjectMapper objectMapper = getMapperInstance(createNew);
			objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
			T vo = objectMapper.readValue(json, cls);
			return vo;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}